<?php
	class articlesModel extends CI_Model
	{
		public $title;
		public $posted;
		public $content;
		
		public function __construct()
		{
			parent::__construct();
		}
		
		public function getAll()
		{
			$this->db->order_by('id',"DESC");
			$query = $this->db->get('articles');
			
			return $query->result();
		}
		
		public function find($id)
		{
			$query = $this->db->get_where('articles', array('id' => $id));
			return $query->row();
		}
		
		public function getLast()
		{
			$this->db->order_by('id',"DESC");
			$query = $this->db->get('articles',1);
			$article = $query->row();
			$article->{"dateFormat"} = date("j M Y", $article->posted);
			$article->{"text"} = htmlspecialchars($article->content);
			
			return $article;
		}
		
		public function getLastNum($num = 5)
		{
			$this->db->order_by('id',"DESC");
			$query = $this->db->get('articles', $num);
			$articles = $query->result();
			
			foreach ($articles as $key => $article) {
				$article->{"dateFormat"} = date("j M Y", $article->posted);
				$article->{"text"} = htmlspecialchars($article->content);
				
				$articles[$key] = $article;
			}
			
			return $articles;
		}
		
		public function insert($title, $content)
		{
			//$title 		= $this->input->post('title');
			$posted		= time();
			//$content	= $this->input->post('content');
			
			if($this->db->insert('articles',array(
				'title'		=>	$title,
				'posted'	=>	$posted,
				'content'	=>	$content
			))) { return true; }
		}
		
		public function update($id, $title, $content)
		{
			//$title 		= $this->input->post('title');
			$posted		= time();
			//$content	= $this->input->post('content');
			
			$this->db->where('id',$id);
			if($this->db->update('articles',array(
				'title'		=>	$title,
				'posted'	=>	$posted,
				'content'	=>	$content
			))) { return true; }
		}
		
		public function remove($id)
		{
			$this->db->delete('articles',array(
				'id'	=>	$id,
			));
			return true;
		}
	}
?>